import { AssetsActions } from "../actions";
import {
  ISelectAssetsRequest,
  ISelectAssetsResponse,
  ISelectCoinConfigResponse,
  IFindAssetsResponse,
  IFindAssetsToBtcResponse,
  IWithdrawResponse,
  ISelectDepositHistoryResponse,
  ISelectWithdrawHistoryResponse,
  ISelectWithdrawAddressResponse,
  IFindDepositAddressResponse
} from "../models/assets";

export interface State {
  selectAssetsResponse: ISelectAssetsResponse;
  selectCoinConfigResponse: ISelectCoinConfigResponse;
  findAssetsResponse: IFindAssetsResponse;
  findAssetsToBtcResponse: IFindAssetsToBtcResponse;
  withdrawResponse: IWithdrawResponse;
  selectDepositHistoryResponse: ISelectDepositHistoryResponse;
  selectWithdrawHistoryResponse: ISelectWithdrawHistoryResponse;
  selectWithdrawAddressResponse: ISelectWithdrawAddressResponse;
  findDepositAddressResponse: IFindDepositAddressResponse;
  pending: boolean;
}

export const initialState: State = {
  selectAssetsResponse: {
    code: -1,
    data: null,
    msg: ""
  },
  selectCoinConfigResponse: {
    code: -1,
    data: null,
    msg: ""
  },
  findAssetsResponse: {
    code: -1,
    data: null,
    msg: ""
  },
  findAssetsToBtcResponse: {
    code: -1,
    data: null,
    msg: ""
  },
  withdrawResponse: {
    code: -1,
    data: null,
    msg: ""
  },
  selectDepositHistoryResponse: {
    code: -1,
    data: null,
    msg: ""
  },
  selectWithdrawHistoryResponse: {
    code: -1,
    data: null,
    msg: ""
  },
  selectWithdrawAddressResponse: {
    code: -1,
    data: [],
    msg: ""
  },
  findDepositAddressResponse: {
    code: -1,
    data: [],
    msg: ""
  },
  pending: false
};

export function reducer(
  state = initialState,
  action: AssetsActions.AssetsActionsUnion
): State {
  switch (action.type) {
    case AssetsActions.AssetsActionTypes.SelectAssets: {
      return {
        ...state,
        pending: true
      };
    }
    case AssetsActions.AssetsActionTypes.SelectAssetsSuccess: {
      return {
        ...state,
        selectAssetsResponse: action.payload.res,
        pending: false
      };
    }

    case AssetsActions.AssetsActionTypes.SelectCoinConfigError: {
      return {
        ...state,
        pending: false
      };
    }

    case AssetsActions.AssetsActionTypes.SelectCoinConfig: {
      return {
        ...state,
        pending: true
      };
    }
    case AssetsActions.AssetsActionTypes.SelectCoinConfigSuccess: {
      return {
        ...state,
        selectCoinConfigResponse: action.payload.res,
        pending: false
      };
    }

    case AssetsActions.AssetsActionTypes.SelectAssetsError: {
      return {
        ...state,
        pending: false
      };
    }

    case AssetsActions.AssetsActionTypes.FindAssets: {
      return {
        ...state,
        pending: true
      };
    }
    case AssetsActions.AssetsActionTypes.FindAssetsSuccess: {
      return {
        ...state,
        findAssetsResponse: action.payload.res,
        pending: false
      };
    }

    case AssetsActions.AssetsActionTypes.FindAssetsError: {
      return {
        ...state,
        pending: false
      };
    }

    case AssetsActions.AssetsActionTypes.FindAssetsToBtc: {
      return {
        ...state,
        pending: true
      };
    }
    case AssetsActions.AssetsActionTypes.FindAssetsToBtcSuccess: {
      return {
        ...state,
        findAssetsToBtcResponse: action.payload.res,
        pending: false
      };
    }

    case AssetsActions.AssetsActionTypes.FindAssetsToBtcError: {
      return {
        ...state,
        pending: false
      };
    }

    case AssetsActions.AssetsActionTypes.Withdraw: {
      return {
        ...state,
        pending: true
      };
    }
    case AssetsActions.AssetsActionTypes.WithdrawSuccess: {
      return {
        ...state,
        withdrawResponse: action.payload.res,
        pending: false
      };
    }

    case AssetsActions.AssetsActionTypes.WithdrawError: {
      return {
        ...state,
        pending: false
      };
    }

    case AssetsActions.AssetsActionTypes.SelectDepositHistory: {
      return {
        ...state,
        pending: true
      };
    }
    case AssetsActions.AssetsActionTypes.SelectDepositHistorySuccess: {
      return {
        ...state,
        selectDepositHistoryResponse: action.payload.res,
        pending: false
      };
    }

    case AssetsActions.AssetsActionTypes.SelectDepositHistoryError: {
      return {
        ...state,
        pending: false
      };
    }

    case AssetsActions.AssetsActionTypes.SelectWithdrawHistory: {
      return {
        ...state,
        pending: true
      };
    }
    case AssetsActions.AssetsActionTypes.SelectWithdrawHistorySuccess: {
      return {
        ...state,
        selectWithdrawHistoryResponse: action.payload.res,
        pending: false
      };
    }

    case AssetsActions.AssetsActionTypes.SelectWithdrawHistoryError: {
      return {
        ...state,
        pending: false
      };
    }

    case AssetsActions.AssetsActionTypes.SelectWithdrawAddress: {
      return {
        ...state,
        pending: true
      };
    }
    case AssetsActions.AssetsActionTypes.SelectWithdrawAddressSuccess: {
      return {
        ...state,
        selectWithdrawAddressResponse: action.payload.res,
        pending: false
      };
    }

    case AssetsActions.AssetsActionTypes.SelectWithdrawAddressError: {
      return {
        ...state,
        pending: false
      };
    }

    case AssetsActions.AssetsActionTypes.FindDepositAddress: {
      return {
        ...state,
        pending: true
      };
    }
    case AssetsActions.AssetsActionTypes.FindDepositAddressSuccess: {
      return {
        ...state,
        findDepositAddressResponse: action.payload.res,
        pending: false
      };
    }

    case AssetsActions.AssetsActionTypes.FindDepositAddressError: {
      return {
        ...state,
        pending: false
      };
    }

    default: {
      return state;
    }
  }
}
